package controllers

import (
	"github.com/astaxie/beego"

	"cvevulner/errcode"
)

type BaseController struct {
	beego.Controller
}

func (c BaseController) response(resp map[string]interface{}) {
	c.Data["json"] = resp
	c.ServeJSON()
	c.StopRun()
}

func (c BaseController) paramError(resp map[string]interface{}) {
	resp["errno"] = errcode.RecodeParamErr
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeParamErr)

	c.response(resp)
}

func (c BaseController) noData(resp map[string]interface{}, err error) {
	resp["code"] = errcode.RecodeNodata
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeNodata)
	if err != nil {
		resp["errmsg"] = err.Error()
	}

	c.response(resp)
}

func (c BaseController) noToken(resp map[string]interface{}) {
	resp["errno"] = errcode.RecodeSessionErr
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeSessionErr)

	c.response(resp)
}

func (c BaseController) tokenValidateFailed(resp map[string]interface{}) {
	c.noToken(resp)
}

func (c BaseController) success(resp map[string]interface{}) {
	resp["code"] = errcode.RecodeOk
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeOk)

	c.response(resp)
}

func (c BaseController) ipFailed(resp map[string]interface{}) {
	resp["errno"] = errcode.RecodeIpErr
	resp["errmsg"] = errcode.RecodeText(errcode.RecodeIpErr)

	c.response(resp)
}
